/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.coreGenerator.base.entitygen.method;

import com.inspur.edp.caf.generator.baseInfo.ParameterInfo;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.method.ClassMethodGenerator;
import com.inspur.edp.cef.api.message.CefException;
import com.inspur.edp.cef.gencommon.MediateType;
import com.inspur.edp.cef.generator.common.DataStaticEqualsMethodGenerator;
import com.inspur.edp.cef.generator.common.MediatePropertyInfo;
import java.sql.ParameterMetaData;
import java.util.ArrayList;
import java.util.List;
import com.inspur.edp.cef.generator.overall.GeneratorUtil;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.ClassInstanceCreation;
import org.eclipse.jdt.core.dom.InfixExpression;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.Modifier;
import org.eclipse.jdt.core.dom.Modifier.ModifierKeyword;
import org.eclipse.jdt.core.dom.ReturnStatement;
import org.eclipse.jdt.core.dom.StringLiteral;
import org.eclipse.jdt.core.dom.SwitchCase;
import org.eclipse.jdt.core.dom.SwitchStatement;
import org.eclipse.jdt.core.dom.ThrowStatement;

public class DataStaticGetValueGenerator extends ClassMethodGenerator {
  private static final String FormalParName = "propName";
  private static final String ParData = "data";
  public static String methodName="getValue";
  private MediateType targetInterfaceType;
  private List<MediatePropertyInfo> propertyInfos;

  public DataStaticGetValueGenerator(MediateType targetInterfaceType,
      List<MediatePropertyInfo> propertyInfos)
  {

    this.targetInterfaceType = targetInterfaceType;
    this.propertyInfos = propertyInfos;
  }

  @Override
  protected String getMethodName() {
    return methodName;
  }

  @Override
  protected TypeInfo getReturnType() {
    addImport(Object.class.getTypeName());
    return new TypeInfo(Object.class.getSimpleName());
  }

  @Override
  protected ArrayList<Modifier.ModifierKeyword> getAccessModifiers() {
    ArrayList<Modifier.ModifierKeyword> list = new ArrayList<>();
    list.add(ModifierKeyword.PUBLIC_KEYWORD);
    list.add(ModifierKeyword.STATIC_KEYWORD);
    return list;
  }

  @Override
  protected ArrayList<ParameterInfo> getParameterCollection() {
    ArrayList<ParameterInfo> list = new ArrayList<>();
    ParameterInfo parData=new ParameterInfo();
    addImport(targetInterfaceType.getFullName());
    parData.setParamType(new TypeInfo(targetInterfaceType.getClassName()));
    parData.setParamName(ParData);
    list.add(parData);

    ParameterInfo parameterInfo = new ParameterInfo();
    parameterInfo.setParamType(new TypeInfo(String.class.getTypeName()));
    parameterInfo.setParamName(FormalParName);
    list.add(parameterInfo);
    return list;
  }

  @Override
  protected Block buildMethodBody() {
    Block block = ast.newBlock();
    SwitchStatement switchStatement = ast.newSwitchStatement();
    setSwitchExpresssion(switchStatement);
    setSwitchCases(switchStatement);
    setSwitchDefaultCase(switchStatement);
    block.statements().add(switchStatement);
    return block;
  }

  private void setSwitchExpresssion(SwitchStatement switchStatement) {
    switchStatement.setExpression(ast.newSimpleName(FormalParName));
  }

  private void setSwitchCases(SwitchStatement switchStatement) {
    for (MediatePropertyInfo propertyInfo : propertyInfos) {
      buildPropertyCaseStatement(propertyInfo, switchStatement);
    }
  }

  private void buildPropertyCaseStatement(MediatePropertyInfo propertyInfo,
      SwitchStatement switchStatement) {
    SwitchCase switchCase1 = ast.newSwitchCase();

    StringLiteral stringLiteral = ast.newStringLiteral();
    stringLiteral.setLiteralValue(propertyInfo.getPropertyName());
    switchCase1.setExpression(stringLiteral);
    switchStatement.statements().add(switchCase1);

    ReturnStatement returnStatement = ast.newReturnStatement();
    MethodInvocation methodInvocation = ast.newMethodInvocation();

    methodInvocation.setName(ast.newSimpleName(propertyInfo.getGetterMethodName()));
    methodInvocation.setExpression(ast.newSimpleName(ParData));
    returnStatement.setExpression(methodInvocation);
    switchStatement.statements().add(returnStatement);
  }

  private void setSwitchDefaultCase(SwitchStatement switchStatement) {
    SwitchCase defaultCase = ast.newSwitchCase();
    defaultCase.setExpression(null);
    switchStatement.statements().add(defaultCase);
    addImport(CefException.class.getTypeName());
    ThrowStatement throwStatement = GeneratorUtil.getThrowStatement(ast,"属性名不存在：",FormalParName,true);
//    ClassInstanceCreation classInstanceCreation = ast.newClassInstanceCreation();
//
//    classInstanceCreation.setType(
//        new com.inspur.edp.caf.generator.baseInfo.TypeInfo(RuntimeException.class).getType(ast));
//
//    InfixExpression infixExpression = ast.newInfixExpression();
//    infixExpression.setOperator(InfixExpression.Operator.PLUS);
//    StringLiteral stringLiteral = ast.newStringLiteral();
//    stringLiteral.setLiteralValue("属性名不存在：");
//    infixExpression.setLeftOperand(stringLiteral);
//    infixExpression.setRightOperand(ast.newSimpleName(FormalParName));
//    classInstanceCreation.arguments().add(infixExpression);
//
//    throwStatement.setExpression(classInstanceCreation);
    switchStatement.statements().add(throwStatement);
  }
}
